<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
<script type="text/javascript">
$(function() {
    $('.date-picker').datepicker( {
        changeMonth: true,
        changeYear: true,
        showButtonPanel: true,
        dateFormat: 'yy-mm',
        onClose: function(dateText, inst) { 
            var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
            var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
             
    		realDate = new Date(year, month, 1);
            $(this).datepicker('setDate', realDate);
        }
    });
   	$('.select').click(function(){
   		var value = $(this).val();
   		if(value=="p"){
   			$('.endRage').show();
   		}else{
   			$('.endRage').val("");
   			$('.endRage').hide();
   		}
   	})
    
});
</script>
<style>
.ui-datepicker-calendar {
    display: none;
    }
</style>


<form id="searchForm" action="<?php echo current_url()?>">
	
	
	
	<?
	
	$arrray['PERNR'] = array("Index"=>"PERNR","Value"=>"พนักงาน");
	$arrray['KUNNR_T'] = array("Index"=>"KUNNR_T","Value"=>"ลูกค้า");
	$arrray['MATNR'] = array("Index"=>"MATNR","Value"=>"รหัสสินค้า");
	$arrray['PRODH3_T'] = array("Index"=>"PRODH3_T","Value"=>"กลุ่มสินค้า");
	$arrray['KDGRP_T'] = array("Index"=>"KDGRP_T","Value"=>"กลุ่มลูกค้า");
	
	?>
	
	<label>
		แบ่งข้อมูลตาม
	</label>
	<select name="groupBy">
	<?
	foreach($arrray as $in=>$val){
		
		$check = ($_GET['groupBy']==$val['Index']) ?'checked="checked"':"";
	?>
		<option <?php echo $check?> value="<?php echo $val['Index']?>"><?php echo $val['Value']?></option>
	<?
	}
	?>
	</select>
	
	<?
	$period = isset($_GET['select']) ? $_GET['select'] :"";
	$style='style="display:none"';
	if($period==""){
		$period = "p";
		
	}
	if($period=="p"){
		$style="";
	}
	?>
	
	<p style="margin-top:10px">
		
		
		<span>ระบุช่วงเวลา</span><br />
		
		<input type="radio" name="select" value="p" <?php echo ($period=="p") ?'checked="checked"':""?>  />
		<label>
		Period
	</label>
	
		<input type="radio" name="select" value="r"  <?php echo  ($period=="r") ?'checked="checked"':""?>/>
		<label>
		Select Month
	</label>
	<br />
	<label>
		เลือกช่วงเวลา
	</label>
	<input type="text" class="inputText date-picker" name="start" value="<? echo (isset($_GET['start'])) ? $_GET['start']:""?>" />
	<input <?php echo $style ?> type="text" class="inputText date-picker endRage" name="end" value="<? echo (isset($_GET['end'])) ? $_GET['end']:""?>" />
	</p>
	<input type="submit" value="ค้นหา" />
</form>
<div class="pagination">
        <ul class="pagination">
            <?
            echo $pagination->create_links();
            ?>
        </ul>    
    </div>

<table class="table table-fixed" style="width:100%"> <!-- cellspacing='0' is important, must stay -->

	<!-- Table Header -->

	<thead>
		<tr>
			<th rowspan="2" style="width:20%" valign="center" align="center">เงื่อนไข</th>
			<?
			$default = "";
			$storeMonth = array();
			$countMaxMount = array();
			foreach($dataList as $in=>$val){
				$check = $val->M."/".$val->Y;
				if($default!=$check){
					$default = $check;
					$storeMonth[] = $check;
					$countMaxMount[$val->M."-".$val->Y] = 1;
					?>
						<th colspan="3"><?php echo $check ?></th>
					<?
				}else{
					$countMaxMount[$val->M."-".$val->Y] =$countMaxMount[$val->M."-".$val->Y]+1; 
					
				}
			}
			
			?>
			
			
		</tr>
		
		<tr>
			<?
			foreach($storeMonth as $in=>$val){
			?>
				<td align="center">ราคา</td>
				<td align="center">จำนวน</td>
				<td align="center">หน่วย</td>
			<?
			}
			?>	
		</tr>
	</thead>
	
	<!-- Table Header -->
	
	<!-- Table Body -->
	<tbody>
<?
	
	function findData($dataList,$month,$year,$max){
		$datas = array();
		foreach($dataList as $in=>$val){
			if($val->M==$month && $val->Y==$year){
				$datas[] = $val;
			}
		}
		$size = count($datas);
		if($size < $max){
			$dif = $max-$size;
			for($i =$size;$i<$max;$i++){
				$datas[$i] = array();
			}
		}
		return $datas;
	}
	$maxOfYear = max($countMaxMount);
	
	$dataOfMonth = array();
	foreach($countMaxMount as $in=>$val){
		list($m,$y) = explode("-", $in);
		$dataOfMonth[$in] = findData($dataList, $m, $y, $maxOfYear);
	}
	
	
	
	for($i=0;$i<$maxOfYear;$i++){
		
	?>
	<tr>
		<?
		foreach($dataOfMonth as $in=>$val ){
			
			if(isset($dataOfMonth[$in][$i]->Con)){
		?>
			<td align="left"><?php echo $dataOfMonth[$in][$i]->Con?>-<?php echo $i?></td>
		<?
			}
	
		?>
			
		<?
		}
	?>
		
		
	<?
	
	foreach($dataOfMonth as $in=>$val ){
		
		if(!empty($dataOfMonth[$in][$i])){
	?>
		<td align="right"><?echo $dataOfMonth[$in][$i]->SUMS?></td>
		<td align="right"><?echo $dataOfMonth[$in][$i]->POSNR?></td>
		<td align="right"><?echo $dataOfMonth[$in][$i]->VRKME?></td>
		
	<?
		}else{
		?>
			<td align="right">-</td>
			<td align="right">-</td>
			<td align="right">-</td>
		<?
		}
	}
	?>
	</tr>
	<?
	}
	?>
	</tbody>
	
	<!-- Table Body -->

</table>
<div class="container">
<div class=" pagination">
        <ul class="pagination">
            <?
			 echo $pagination->create_links();
            ?>
        </ul>    
    </div>
    
